/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany.delivery.tableModels;

import com.mycompany.delivery.dto.DeliveryDTO;
import com.mycompany.delivery.dto.UserDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Jana Nemcova
 */
public class DeliveryTableModel2 extends AbstractTableModel {
    private List<DeliveryDTO> deliveries;
    RestTemplate restTemplate = new RestTemplate();
    //private java.util.ResourceBundle bundle;
 
    public DeliveryTableModel2() {
       deliveries = new ArrayList<DeliveryDTO>(); 
     //   bundle = java.util.ResourceBundle.getBundle("Locales/localization");
    }
    
    @Override
    public int getRowCount() {
        return deliveries.size();
    }
 
    
    @Override
    public int getColumnCount() {
        return 4;
    }
 
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        DeliveryDTO delivery = deliveries.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return delivery.getId();
            case 1:
                if (delivery.getSender()==null) {
                    return "--";
                }
                return delivery.getSender().getName()+" "+delivery.getSender().getSurname();
            case 2:
                if (delivery.getReceiver()==null) {
                    return "--";
                }
                return delivery.getReceiver().getName()+" "+delivery.getReceiver().getSurname();
                
            case 3:
                return delivery.getSendingDate().toString();
           
            default:
                throw new IllegalArgumentException("error with column index");
        }
    }
    
    public DeliveryDTO getDeliveryAt(int index){
        return deliveries.get(index);
    }
    
    public void initDeliveries() {
        InitDeliveriesWorker initDeliveriesWorker = new InitDeliveriesWorker();
        initDeliveriesWorker.execute();
    }
    
    public void addDeliveryToCourier(DeliveryDTO delivery, Long courier) {
        AddDeliveryToCourierWorker addWorker = new AddDeliveryToCourierWorker();
        addWorker.setDelivery(delivery);
        addWorker.setUser(courier);
        addWorker.execute();
    }
/*
    public void updateDelivery(DeliveryDTO delivery) {
        UpdateDeliveryWorker updateDeliveryWorker = new UpdateDeliveryWorker();
        updateDeliveryWorker.setDelivery(delivery);
        updateDeliveryWorker.execute();
    }
    
    public void deleteDelivery(DeliveryDTO delivery) {
        DeleteDeliveryWorker deleteDeliveryWorker = new DeleteDeliveryWorker();
        deleteDeliveryWorker.setDelivery(delivery);
        deleteDeliveryWorker.execute();
    }
    */
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Long.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return String.class;

            default:
                throw new IllegalArgumentException("error with column class");
        }
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "ID";
            case 1:
                return "SENDER";
            case 2:
                return "RECEIVER";
            case 3:
                return "SENDING DATE";

            default:
                throw new IllegalArgumentException("error with column name");
        }
    }
    private class InitDeliveriesWorker extends SwingWorker<List<DeliveryDTO>,Void> {
        
        @Override    
        protected List<DeliveryDTO> doInBackground() throws Exception {
            List<DeliveryDTO> listOfDel = new ArrayList<DeliveryDTO>();
           try {
            DeliveryDTO[] deliveries = restTemplate.getForObject("http://localhost:8080/pa165/deliveries/getAllDeliveriesWithoutCourierJson", DeliveryDTO[].class);
            for (DeliveryDTO del : deliveries) {
                listOfDel.add(del);
            }
        } catch (HttpClientErrorException e) {
            System.err.println(e.getMessage());
            }
         catch (ResourceAccessException ex)
         {
          //NEJDE WEB, JIŽ OZNÁMENO
             
         }
           return listOfDel;
        }
        
        @Override
        protected void done(){
            try {
                deliveries.clear();
                for(DeliveryDTO del : get()){
                    deliveries.add(del);
                }
                fireTableChanged(null);
            } catch (ExecutionException ex) {
                throw new RuntimeException(ex);
            } catch (InterruptedException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
   
    private class AddDeliveryToCourierWorker extends SwingWorker<Void,Void> {
        
        private DeliveryDTO delivery;
        private Long user;
        private DeliveryDTO deliveryUpdated;

        public void setUser(Long user) {
            this.user = user;
        }

        public void setDelivery(DeliveryDTO delivery) {
            this.delivery = delivery;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            deliveryUpdated = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/addDeliveryToCourier/{idCourier}", delivery, DeliveryDTO.class, user);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
        
        @Override
        protected void done(){
            deliveries.remove(deliveryUpdated);
            fireTableChanged(null);
        }
    }
    /*
    private class UpdateDeliveryWorker extends SwingWorker<Void,Void> {
        
        private DeliveryDTO delivery;

        public void setDelivery(DeliveryDTO delivery) {
            this.delivery = delivery;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            delivery = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/update", delivery, DeliveryDTO.class);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
        @Override    
        protected void done() {
            for(DeliveryDTO del: deliveries){
                if(del.getId() == delivery.getId()){
                    del = delivery;
                    break;
                }
            }
            fireTableChanged(null);
        }
    }*/
    /*
    private class DeleteDeliveryWorker extends SwingWorker<Void,Void> {
        
        private DeliveryDTO delivery;

        public void setDelivery(DeliveryDTO delivery) {
            this.delivery = delivery;
        }
        
        @Override    
        protected Void doInBackground() throws Exception {
            try {
            restTemplate.postForObject("http://localhost:8080/pa165/deliveries/delete", delivery, DeliveryDTO.class);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());
          }
            return null;
        }
    
        @Override    
        protected void done() {
            deliveries.remove(delivery);
            fireTableChanged(null);
        }
    }*/
}
